#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>

#define FOR(i,a,n) for(int i=a;i<n;++i)
#define REP(i,n) FOR(i,0,n)

using namespace std;

int row,col;
char s[10010][80];
char cs[10010];
int next[10010];
int GetShortest(char* str,int len)
{
    next[0]=-1;
    int j=0;
    FOR(i,1,len)
    {
        while(j&&str[i]!=str[j]) j=next[j-1]+1;
        if(str[i]==str[j]) next[i]=j++;
        else next[i]=-1;
    }
    return len-1-next[len-1];
}
int lcm(int a,int b)
{
    return a*b/__gcd(a,b);
}
int main()
{
    while(cin>>row>>col)
    {
        REP(i,row) scanf("%s",s[i]);
        int cl,rl;
        cl=rl=1;
        REP(i,row)
        {
            cl=lcm(cl,GetShortest(s[i],col));
            if(cl>col)
            {
                cl=col;
                break;
            }
        }
        REP(i,col)
        {
            REP(j,row) cs[j]=s[j][i];
            rl=lcm(rl,GetShortest(cs,row));
            //printf("col %d: %d\n",i,rl);
            if(rl>=row)
            {
                rl=row;
                break;
            }
        }
        cout<<cl*rl<<endl;
    }
    return 0;
}
/*
2 8
aaaabbaa
aaaabbaa

*/
